import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_lang/lang_index.dart';
import 'package:wallet_home/tools/theme_utils.dart';

import '../../module_config.dart';

class SearchBar extends StatefulWidget {
  final FocusNode focusNode;
  final bool enabled;
  final bool hideLeft;
  final bool hideRight;
  final String hint;
  final String defaultText;
  final void Function() leftButtonClick;
  final void Function() rightButtonClick;
  final void Function() inputBoxClick;
  final ValueChanged<String> onChanged;
  final Widget rightWidget;

  const SearchBar(
      {Key key,
      this.focusNode,
      this.enabled = true,
      this.hideLeft = false,
      this.hideRight = false,
      this.hint,
      this.defaultText,
      this.leftButtonClick,
      this.rightButtonClick,
      this.inputBoxClick,
      this.onChanged,
      this.rightWidget})
      : super(key: key);

  @override
  _SearchBarState createState() => _SearchBarState();
}

class _SearchBarState extends State<SearchBar> {
  final TextEditingController _controller = TextEditingController();

  @override
  void initState() {
    if (widget.defaultText != null) {
      setState(() {
        _controller.text = widget.defaultText;
      });
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return _genNormalSearch();
  }

  _genNormalSearch() {
    return Container(
      margin: EdgeInsets.only(top:MediaQuery.of(context).padding.top),
      child: Row(children: <Widget>[
        _wrapTap(
            Container(
              margin: EdgeInsets.fromLTRB(15, 10, 20, 5),
              child: widget?.hideLeft ?? false
                  ? null
                  : Icon(
                      Icons.arrow_back,
                      color: ThemeUtils.getThemeColor(
                          context: context,
                          brightnessColor: Colors.grey[300],
                          darkColor: Colors.white),
                    ),
            ),
            widget.leftButtonClick),
        Expanded(
          flex: 1,
          child: Padding(
            padding: EdgeInsets.only(top: 5),
            child: _inputBox(),
          ),
        ),
        _wrapTap(
            Container(
              margin: EdgeInsets.fromLTRB(10, 8, 10, 5),
              child: _rightWidget(),
            ),
            widget.rightButtonClick)
      ]),
    );
  }

  _rightWidget() {
    return widget.hideRight
        ? null
        : widget.rightWidget ??
            Text(
              '${WXLang.$t("search_cancel",package: ModuleConfig.moduleName)}',
              style: TextStyle(color: Colors.grey, fontSize: 17),
            );
  }

  _inputBox() {
    Color inputBoxColor;
    inputBoxColor = ThemeUtils.getThemeColor(
        context: context,
        brightnessColor: Color(0xFFEEEEEE),
        darkColor: Colors.grey[600]);
    return Container(
      height: 30,
      margin: EdgeInsets.fromLTRB(0, 0, 10, 0),
      padding: EdgeInsets.only(left: 10),
      decoration: BoxDecoration(
          color: inputBoxColor, borderRadius: BorderRadius.circular(15)),
      child: Row(
        children: <Widget>[
          Icon(Icons.search, size: 20, color: Color(0xffA9A9A9)),
          Expanded(
              flex: 1,
              child: TextField(
                focusNode: widget.focusNode,
                enabled: widget.enabled,
                controller: _controller,
                onChanged: _onChanged,
                autofocus: false,
                cursorColor: Colors.grey,
                style: TextStyle(
                  height: Platform.isIOS ? 1.1 : 1.3,
                  textBaseline: TextBaseline.alphabetic,
                    fontSize: 18.0,
                    color: ThemeUtils.getThemeColor(
                        context: context,
                        brightnessColor: Colors.grey[800],
                        darkColor: Colors.grey[400]),
                    fontWeight: FontWeight.w300),
                //输入文本的样式
                decoration: InputDecoration(
                  contentPadding:
                      EdgeInsets.symmetric(vertical:Platform.isIOS ? 12.5 : 11, horizontal: 5),
                  border: InputBorder.none,
                  hintText: widget.hint ?? '',
                  hintStyle: TextStyle(
                      height: Platform.isIOS ? 1.1 : 1.3,
                      fontSize: 15,
                      color: Colors.grey[400]
                  ),
                  labelStyle: TextStyle(
                    height: 1.5,
                    fontWeight: FontWeight.w100
                  )
                ),
                textAlignVertical: TextAlignVertical.center,
              )),
        ],
      ),
    );
  }

  _wrapTap(Widget child, void Function() callback) {
    return GestureDetector(
      onTap: () {
        if (callback != null) callback();
      },
      child: child,
    );
  }

  _onChanged(String text) {
    if (widget.onChanged != null) {
      widget.onChanged(text);
    }
  }
}
